//
//  HHFileMessageCell.m
//  HHChatKit
//
//  Created by Henry on 2021/6/3.
//

#import "HHFileMessageCell.h"
#import <HHTool/HHTool.h>
#import "HHResources.h"

@implementation HHFileMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        _fileNameLabel = [[UILabel alloc] init];
        _fileNameLabel.numberOfLines = 2;
        [self.container addSubview:_fileNameLabel];

        _lengthLabel = [[UILabel alloc] init];
        [self.container addSubview:_lengthLabel];

        _fileImageView = [[UIImageView alloc] init];
        _fileImageView.image = [HHResources getImageFromMessage:@"msg_file"];
        _fileImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.container addSubview:_fileImageView];
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor hh_colorWithString:@"#DDDDDD"];
        [self.container addSubview:_lineView];
        
        _flagImageView = [[UIImageView alloc] init];
        _flagImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.container addSubview:_flagImageView];

        _flagLabel = [[UILabel alloc] init];
        [self.container addSubview:_flagLabel];
        
        _progressView = [[DACircularProgressView alloc] init];
        _progressView.roundedCorners = YES;
        [self.container addSubview:_progressView];
    }
    return self;
}

- (void)fillWithData:(HHFileMessageCellData *)data {
    [super fillWithData:data];
    self.fileData = data;
    _fileNameLabel.text = data.fileName;
    _lengthLabel.text = [self formatLength:data.length];
    
    _flagImageView.image = data.flagImage;
    _flagLabel.text = data.flagName ?: @"";
    
    
    _fileNameLabel.font = data.fileNameFont;
    _fileNameLabel.textColor = data.fileNameColor;
    _lengthLabel.font = data.lengthFont;
    _lengthLabel.textColor = data.lengthColor;
    _flagLabel.font = data.flagFont;
    _flagLabel.textColor = data.flagColor;
    _lineView.backgroundColor = data.lineColor;

    
    [self.fileData addObserver:self forKeyPath:@"uploadProgress" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial context:nil];
    [self.fileData addObserver:self forKeyPath:@"downladProgress" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial context:nil];

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"uploadProgress"]) {
        CGFloat progress = self.fileData.uploadProgress / 100.f;
        [self.progressView setProgress:progress animated:YES];
        self.progressView.hidden = (progress >= 1 || progress == 0);

    } else if ([keyPath isEqualToString:@"downladProgress"]) {
        CGFloat progress = self.fileData.downladProgress / 100.f;
        [self.progressView setProgress:progress animated:YES];
        self.progressView.hidden = (progress >= 1 || progress == 0);
    }
}

- (NSString *)formatLength:(long)length {
    double len = length;
    NSArray *array = [NSArray arrayWithObjects:@"Bytes", @"K", @"M", @"G", @"T", nil];
    int factor = 0;
    while (len > 1024) {
        len /= 1024;
        factor++;
        if(factor >= 4){
            break;
        }
    }
    return [NSString stringWithFormat:@"%4.2f%@", len, array[factor]];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize containerSize = [self.fileData contentSize];
    
    CGFloat contentWidth = containerSize.width - self.fileData.cellLayout.bubbleInsets.left - self.fileData.cellLayout.bubbleInsets.right;
    
    if (self.fileData.flagName.length == 0) {
        _flagImageView.hidden = YES;
        _flagLabel.hidden = YES;
        _lineView.hidden = YES;
    } else {
        _flagImageView.hidden = NO;
        _flagLabel.hidden = NO;
        _lineView.hidden = NO;
    }
    
    CGSize flagSize = [_flagLabel sizeThatFits:containerSize];
    CGFloat flagTopMargin = 4;
    CGFloat flagBottomMargin = 6;
    CGFloat flagY = containerSize.height - flagSize.height - flagBottomMargin;
        
    CGFloat flagImageWidth = self.fileData.flagImage ? flagSize.height : 0;
    _flagImageView.frame = CGRectMake(self.fileData.cellLayout.bubbleInsets.left, flagY, flagImageWidth, flagImageWidth);
    CGFloat flagWidth = contentWidth - _flagImageView.mm_maxX - flagTopMargin;
    _flagLabel.frame = CGRectMake(_flagImageView.mm_maxX + flagTopMargin, flagY, flagWidth, flagSize.height);
    CGFloat lineY = flagY - flagTopMargin - 1/[UIScreen mainScreen].scale;
    _lineView.frame = CGRectMake(self.fileData.cellLayout.bubbleInsets.left, lineY, contentWidth, 1/[UIScreen mainScreen].scale);
    
    CGFloat topHeight = self.fileData.flagName.length == 0 ? containerSize.height : lineY;
    
    CGFloat imageHeight = topHeight - self.fileData.cellLayout.bubbleInsets.top - self.fileData.cellLayout.bubbleInsets.bottom;
    CGFloat imageWidth = imageHeight;
    
    _fileImageView.frame = CGRectMake(containerSize.width - self.fileData.cellLayout.bubbleInsets.right - imageWidth, self.fileData.cellLayout.bubbleInsets.top, imageWidth, imageHeight);
    
    CGFloat textWidth = _fileImageView.frame.origin.x - self.fileData.cellLayout.bubbleInsets.left - self.fileData.cellLayout.bubbleInsets.right;
    CGSize nameSize = [_fileNameLabel sizeThatFits:containerSize];
    _fileNameLabel.frame = CGRectMake(self.fileData.cellLayout.bubbleInsets.left, self.fileData.cellLayout.bubbleInsets.top, textWidth, nameSize.height);
    
    CGSize lengthSize = [_lengthLabel sizeThatFits:containerSize];
    _lengthLabel.frame = CGRectMake(self.fileData.cellLayout.bubbleInsets.left, _fileNameLabel.frame.origin.y + nameSize.height + 4, textWidth, lengthSize.height);
    
    self.progressView.mm_center();
    
}

- (void)dealloc {
    if (self.fileData) {
        [self.fileData removeObserver:self forKeyPath:@"uploadProgress" context:nil];
        [self.fileData removeObserver:self forKeyPath:@"downladProgress" context:nil];
    }
}

@end
